// pages/addressList/addressList.js
var request = require('../../utils/https.js');
var uri = 'address/index.html';
var del = 'address/del.html';
var setdef = 'address/status.html';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],
    showData:false
  },
  selectList:function(e){
    const index = e.currentTarget.dataset.index;
    let address = this.data.addressList;
    request.req(setdef, {
      user_id: wx.getStorageSync('userId'),
      id:address[index].id
    }, (err, res) => {
      if(res.data){
        for (var i in address) {
          if (i == index) {
            address[i].status = 1;
          } else {
            address[i].status = 0;
          }
        }
        this.setData({
          addressList: address
        });
      }else{
        wx.showToast({
          title: '删除失败',
          icon: 'none',
        })
      }
    })

  },
  editAddr:function(e){
    var item = e.currentTarget.dataset.item;
    var options = '?way=edit&id=' + item.id + "&name=" + item.real_name + "&tel=" + item.mobile + "&address=" + item.address + "&detail=" + item.address_item
    wx.navigateTo({
      url: '../addressAdd/addressAdd'+options,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  delList:function(e){
    const index = e.currentTarget.dataset.index;
    let address = this.data.addressList;
    console.log(address[index].id)
    request.req(setdef, {
      user_id: wx.getStorageSync('userId'),
      id: address[index].id
    }, (err, res) => {
      if(res.data){
        address.splice(index, 1);
        this.setData({
          addressList: address
        });
        if (!address.length) {
          this.setData({
            showData: false
          });
        }
      }else{
        wx.showToast({
          title: '删除失败',
          icon: 'none',
        })
      }
    })
  },
  getData: function () {
    var that = this;
    request.req(uri, {
      user_id: wx.getStorageSync('userId')
    }, (err, res) => {
      if (res.data.list.length>0){
        that.setData({
          showData: true,
          addressList: res.data.list
        })
      }else{
        that.setData({
          showData: false,
        })
      }
    })
  },
  onShow: function () {
    this.getData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})